import cgi
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from entities import *
from Entities import *
from entities import User

def Add(userID,countryID,address1,address2,city,province,postalCode,company,website,ppAccount):
    try:
        user = Entities.User.get_by_id(userID)
        country = Country.get_by_id(countryID)
        developer = Developer(UserID = user,CountryID = country,Address1 = address1,Address2 = address2,City = city,Province = province,PostalCode = postalCode,Company = company,Website = website,PaypalAccount = ppAccount)
        developer.put()
        return 1
    except:
        return 0
    
def GetbyName(userName):
    developer_list = []
    developers = Developer.gql('WHERE UserName = :1',userName)
    for developer in developers:
        developer_dict = {}
        developer_dict["DeveloperID"] = developer.key().id()
        developer_dict["UserName"]= developer.UserName
        developer_dict["CountryName"] = developer.CountryID.CountryName
        developer_dict["Address1"] = developer.Address1
        developer_dict["Address2"] = developer.Address2
        developer_dict["City"] = developer.City
        developer_dict["Province"] = developer.Province
        developer_dict["PostalCode"] = developer.PostalCode
        developer_dict["Company"] = developer.Company
        developer_dict["Website"] = developer.Website
        developer_dict["PaypalAccount"] = developer.PaypalAccount
        developer_list.append(developer_dict)
    tuple_developer = tuple(developer_list)
    return tuple_developer
    
def Update(DeveloperID,userName,countryID,address1,address2,city,province,postalCode,company,website,ppAccount):
    developer_update = Developer.get_by_id(DeveloperID)
    country = Country.get_by_id(countryID)
    developer = db.GqlQuery("SELECT * FROM Developer WHERE UserName = :1",UserName)
    if developer.count() == 0:
        developer_update.UserName = userName
        developer_update.CountryID = country
        developer_update.Address1 = Address1
        developer_update.Address2 = Address2
        developer_update.City = City
        developer_update.Province = province
        developer_update.PostalCode = postalCode
        developer_update.Company = company
        developer_update.Website = website
        developer_update.PaypalAccount = ppAccount
        developer_update.put()
        return "Complete"
    else:
        return "Duplicate"
        
def Delete(DeveloperID):
    developer = Developer.get_by_id(DeveloperID)
    if developer.count()>0:
        Developer(developer)
        return "Complete"
    else:
        return "No ..."
        
def GetID(userName):
    user = users.get_current_user()
    userID = User.GetID(user)
    userkey = Entities.User.get_by_id(userID)
    devs = Entities.Developer.gql('WHERE UserID = :1',userkey)
    if devs.count() != 0:
        for dev in devs:
            devID = dev.key().id()
            return devID
    else:
        return None
        